package com.briup.service.Impl;

import com.briup.bean.*;
import com.briup.dao.IOrderDao;
import com.briup.dao.IOrderItemDao;
import com.briup.service.IOrderService;
import com.briup.service.IShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class IOrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private IOrderItemDao orderItemDao;
    @Autowired
    private IShopCarService shopCarService;

    @Override
    public List<Order> findUserAllOrders(Long userId) {
        return orderDao.findByUserId(userId);
    }

    @Override
    public void saveOrder(Long[] shopCarIds, User user, Long addressId,String id, BigDecimal sumPrice) {
        ShippingAddress shippingAddress = new ShippingAddress();
        shippingAddress.setId(addressId);
        Order order = new Order(user,shippingAddress,id);
        List<OrderItem> orderItemList = new ArrayList<>();
        List<ShopCar> shopCarList = shopCarService.findShopCars(shopCarIds);
        shopCarList.forEach(shop->{
            OrderItem orderItem = new OrderItem(shop);
            orderItem.setOrderId(id);
            orderItemList.add(orderItem);
        });
        //order.setShopNum(shopCarList.size());
        //order.setSumPrice(sumPrice);
        order.setStatus(1);
        order.setOrderItems(orderItemList);
        //System.out.println(order);
        orderDao.saveOrder(order);
        //System.out.println(order.getOrderItems());
        orderItemDao.saveOrderItem(order.getOrderItems());
    }

    @Override
    public void paySuccess(String orderId) {
        orderDao.updateOrder(orderId);
    }

    @Override
    public Order findById(String orderId) {
        Order order = orderDao.findOrderByOrderId(orderId);
        //System.out.println(order.getOrderItems());
        return order;
    }

    @Override
    public void deleteOrder(String orderId) {
        //查出orderItemList
        //Order order = findById(orderId);
        //List<OrderItem> orderItemList = order.getOrderItems();
        orderItemDao.deleteOrderItem(orderId);
        orderDao.deleteOrder(orderId);
    }
}
